package game.source.gui.shot;

import game.source.gui.GuiComponent;
import game.source.shot.GuiShotInterface;

import java.awt.Point;
import java.awt.image.BufferedImage;

/**
 * GuiShot:
 * 	Gerencia a parte gráfica dos tiros
 */
public abstract class  GuiShot extends GuiComponent implements GuiShotInterface{
	private CallBackShotInterface	shot;
	private Point					position;		//Posição em que o tiro está
	private double					vel;			//velocidade do tiro
	private double					accumulator;	//Acumulador para determinar quanto se deve andar no frame

	@Override
	public void setShot(CallBackShotInterface shot) {
		this.shot = shot;		
	}

	@Override
	public void setStartPosition(Point position) {
		this.position = position;

	}

	@Override
	public void setVel(double vel) {
		this.vel = vel;
	}

	public abstract BufferedImage getImage();

	@Override
	public void show(long time) {
		this.accumulator += this.vel*time/1000;
		if(this.accumulator >= 1){
			if(this.shot.getShooter() == Shooter.PLAYER){
				this.position.y -= (int)this.accumulator;
			}else{
				this.position.y += (int)this.accumulator;
			}
			this.accumulator -= (int)this.accumulator;
		}
		if(this.shot.getShooter() == Shooter.PLAYER){
			this.getGraphics().drawImage(this.getImage(), this.position.x, this.position.y, null);	
		}else{
			this.getGraphics().drawImage(this.getImage(), this.position.x, this.position.y, this.position.x+this.getImage().getWidth(), this.position.y+this.getImage().getHeight(),
					0, this.getImage().getHeight(), this.getImage().getWidth(), 0, null);
		}
	}


	@Override
	public int getX0(){
		return this.position.x;
	}


	@Override
	public int getY0(){
		return this.position.y;
	}


	@Override
	public int getWidth(){
		return this.getImage().getWidth();
	}


	@Override
	public int getHeight(){
		return this.getImage().getHeight();
	}

	@Override
	public byte	getPixelAlpha(int x,int y){
		int pixel = this.getImage().getRGB(x,y);
		byte alpha = (byte)((pixel >> 24) & 0xFF);
		return alpha;
	}

}
